package PMCXMLParser;


import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import javax.management.Query;


public class Download_Documents
{

    private List<String> pmc_ids = new ArrayList<String>();
    
    File_Manager_Impl fileManager = new File_Manager_Impl();

  

    
  /*  public void read_pmcids_from_search_result_file(String input_file)
    {
        StringBuffer strBufPMCIDS = fileManager.fileReader(input_file);
        StringTokenizer st1 = new StringTokenizer(strBufPMCIDS.toString(), "\r\n");
        while(st1.hasMoreElements())
        {
            pmc_ids.add(st1.nextToken());
        }
    }
*/
    
    public void download_pmcids()
    {
        
        StringBuffer strBufPMCIDS = fileManager.fileReader("E:\\URL.txt");
        StringTokenizer st1 = new StringTokenizer(strBufPMCIDS.toString(), "\n");
        while(st1.hasMoreElements())
        {
           String Token=st1.nextToken();
            System.out.println("READ DOWLNLOD :"+Token);
            fileManager.fileDownloader(Token,"E:\\PMCXML\\"+Token+".txt");
        }
       
    }

 

    public void download_pmcids(String input_file) 
    {
      

         fileManager.URLGenerator(input_file);
        // download_pmcids();
    }

    
     
     

    public void readOWL()
    {
        
         StringBuffer DBpedia = new StringBuffer();
         StringBuffer PURL = new StringBuffer();
        StringBuffer contents = null;

        contents = fileManager.fileReader("E:\\diseases_tcm_dbpedia_simple.owl");

        
        int index = 0;
        int counter=1;
        String pmc_id = "";
        Boolean Bfirst=false,BSecond=false,Bthird=false;
        Bfirst=true;
        while((index = contents.indexOf("<", index)) != -1)
        {
            index ++;
            String remaining = contents.substring(index);
            StringTokenizer st = new StringTokenizer(remaining, ">");
            pmc_id = st.nextToken();
           // System.out.println("First "+Bfirst + "Second " + BSecond +"Third"+ Bthird );
               if (Bfirst)
               {   
                   Bfirst=false;
                   BSecond=true;
                   Bthird=false;
                   System.out.println(counter+"    "+pmc_id );
                    DBpedia.append(pmc_id);
                    DBpedia.append(System.getProperty("line.separator"));
                    String outputFileName="E:\\DBpedia.txt";
                    fileManager.fileWriter(DBpedia,  outputFileName);
               }
               else if (BSecond)
               {   
                   Bfirst=false;
                   BSecond=false;
                   Bthird=true;
               }
               
              else   if (Bthird)
               {   
                   Bfirst=true;
                   BSecond=false;
                   Bthird=false;
                   System.out.println(counter+"    "+pmc_id );
                    PURL.append(pmc_id);
                    PURL.append(System.getProperty("line.separator"));
                    String outputFileName="E:\\PURL.txt";
                    fileManager.fileWriter(PURL,  outputFileName);
                   
               }
             counter++;
 
        }
    }

    
  

}
